import { BadGatewayException, Body, Controller, Ip, Post, Req } from '@nestjs/common';
import { LoginService } from './login.service';
import { loginbyphoneDto, sendphoneDto } from './loginDto/loginbDto';


@Controller('login')
export class LoginController {

    constructor(private loginService: LoginService) { }

    /**
     * @Description: 发送验证码
     * @param phone {String} 手机号
     * @return: Promise
     */
    @Post('sendphone')
    async sendphone(@Body() body: sendphoneDto, @Req() req) {
        try {
            if (req.method == 'POST') {
                return await this.loginService.sendphone(body)
            }
        } catch (error) {
            throw new BadGatewayException(error)
        }
    }



    /**
    * @Description: 登录接口
    * @param phone {String} 手机号
    * @param phonecode {String} 手机验证码 
    * @return: Promise
    */
    @Post('loginbyphonecode')
    async loginbyphone(@Body() body: loginbyphoneDto, @Ip() ip, @Req() req) {
        try {
            return await this.loginService.loginbyphone(body, ip.match(/\d+.\d+.\d+.\d+/)[0], req)
        } catch (error) {
            throw new BadGatewayException(error)
        }
    }

}
